package com.webcash.CNBoard.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.EmpBoard;
import com.webcash.CNBoard.Model.EmpComment;

@Repository
public class BoardRepositoryImpl extends SqlSessionDaoSupport implements
		BoardRepository {

	public List<EmpBoard> selectEmpBoardListByEmpNo(int empNo, int pageNum) {
		SqlSession session = getSqlSession();
		RowBounds rowBounds = new RowBounds(10 * (pageNum - 1),10);
		return session.selectList("EmpBoard.selectEmpBoardListByEmpNo", empNo,rowBounds);
	}

	public int selectEmpBoardCountByEmpNo(int empNo) {
		SqlSession session = getSqlSession();
		return session.selectOne("EmpBoard.selectEmpBoardCountByEmpNo", empNo);
	}



	public void insertEmpBoardByEmpNo(String id, Board board, int empNo,
			int empBoardNo) {
		SqlSession session = getSqlSession();

		EmpBoard empBoard = new EmpBoard();
		empBoard.setEmpNo(empNo);
		empBoard.setEmpBoardNo(empBoardNo);
		empBoard.setTitle(board.getTitle());
		empBoard.setArticle(board.getArticle());
		empBoard.setCommentCount(0);
		empBoard.setReadCount(0);
		empBoard.setWriteDate(new Date());
		empBoard.setModifyDate(new Date());
		empBoard.setId(id);
		
		session.insert("EmpBoard.insertEmpBoardByEmpNo", empBoard);
	}
	
	

	
	

	public void updateEmpBoardByEmpNo(Board board, int empBoardNo, int empNo) {
		SqlSession session = getSqlSession();
		Map params = new HashMap();
		params.put("title", board.getTitle());
		params.put("article", board.getArticle());
		params.put("empNo", empNo);
		params.put("empBoardNo", empBoardNo);
		params.put("modifyDate", new Date());
		System.out.println("haha" + empBoardNo);
		
		session.update("EmpBoard.updateEmpBoardByEmpNo", params);
	}

	public int selectEmpBoardEmpBoardNoMaxByEmpNo(int empNo) {

		SqlSession session = getSqlSession();
		Map map = session.selectOne(
				"EmpBoard.selectEmpBoardEmpBoardNoMaxByEmpNo", empNo);
		
		if(map == null){
			return 0;
		}else{
			int empBoardNo = (Integer) map.get("maxEmpBoardNo");
			return empBoardNo;
		}
	}

	public EmpBoard selectEmpBoardByEmpNoAndEmpBoardNo(int empNo, int empBoardNo) {
		SqlSession session = getSqlSession();

		Map params = new HashMap();
		params.put("empNo", empNo);
		params.put("empBoardNo", empBoardNo);

		System.out.println("EmpBoard : select empBoard / empNo:" + empNo
				+ " / empBoardNo:" + empBoardNo);
		return session.selectOne("EmpBoard.selectEmpBoardByEmpNoAndEmpBoardNo",
				params);
	}

	public void deleteEmpBoardByEmpNoAndEmpBoardNo(int empNo, int empBoardNo, int boardNo) {
		SqlSession session = getSqlSession();

		Map params = new HashMap();
		params.put("empNo", empNo);
		params.put("empBoardNo", empBoardNo);

		session.delete("EmpBoard.deleteEmpBoardByEmpNoAndEmpBoardNo", params);
		
		session.delete("EmpBoard.deleteEmpCommentByBoardNo", boardNo);

	}

	public List<EmpComment> selectEmpCommentListByBoardNo(int boardNo) {
		SqlSession session = getSqlSession();
		
		return  session.selectList("EmpBoard.selectEmpCommentListByBoardNo", boardNo);
	}

	public void insertEmpCommentByBoardNoAndId(int boardNo, String id, String commentContents) {
		SqlSession session = getSqlSession();
		
		EmpComment empComment = new EmpComment();
		empComment.setBoardNo(boardNo);
		empComment.setId(id);
		empComment.setCommentLevel(0);
		empComment.setWriteDate(new Date());
		empComment.setCommentContents(commentContents);
		
		session.insert("EmpBoard.insertEmpCommentByBoardNoAndId", empComment);
		
	}
	
	

	public void updateEmpBoardForCommentCountByBoardNo(int boardNo,
			int commentCount) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		params.put("boardNo", boardNo);
		params.put("commentCount", commentCount);
		session.update("EmpBoard.updateEmpBoardForCommentCountByBoardNo", params);
	}

	public EmpBoard selectEmpBoardByBoardNo(int boardNo) {
		SqlSession session = getSqlSession();
		return session.selectOne("EmpBoard.selectEmpBoardByBoardNo", boardNo);
	}

	public void updateEmpBoardForReadCountByBoardNo(int boardNo, int readCount) {
		SqlSession session = getSqlSession();
		Map params = new HashMap();
		params.put("boardNo", boardNo);
		params.put("readCount", readCount);
		session.update("EmpBoard.updateEmpBoardForReadCountByBoardNo", params);
	}

	
	

}
